<?php
    namespace Home\Controller;
    use Think\Controller;
    class RoleController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $list = M("comm.sys_role_list")->select();
        
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = count($list);
		
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("comm.sys_users")->where("uuid = '$uuid'")->find();
		unset($info['pass_word']);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);
		
		if(empty($data['pass_word']))
		{
			unset($data['pass_word']);
		}
		else
		{
			$data['pass_word'] = md5($data['pass_word']);
		}
		
        $result = M("comm.sys_users")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
		
        $data['uuid'] = $uuid;
        
        $result = M("comm.sys_role_list")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function set_field()
    {
		$uuid = I('post.uuid');
		$role_name = I('post.role_name');
        
        $data['role_name'] = $role_name;
		
        $result = M('comm.sys_role_list')->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
        $this->ajaxReturn($json,"json");
    }
	
	public function get_role_menu_list()
	{
	    $uuid = I('post.uuid');
		$role_menu_list = M('comm.sys_role_menu_list')->where("role_id = $uuid")->select();
		
		$menu_list = M('comm.sys_menu_list')->select();
		$col_list = M('comm.sys_col_list')->select();
		
		
		
		for($i = 0 ; $i < count($menu_list) ; $i++)
		{
			$menu_col_list = explode(",",$menu_list[$i]['col_id_list']);
			for($j = 0 ; $j < count($menu_col_list);$j++)
			{
				$menu_list[$i]['col_list'][$j]["id"] = $menu_col_list[$j];
				$menu_list[$i]['col_list'][$j]["check"] = false;
			}
		}
		
		for($i = 0 ; $i < count($menu_list) ; $i++)
		{
			for($j = 0 ; $j < count($menu_list[$i]['col_list']) ; $j++)
			{
				for($k = 0 ; $k < count($col_list) ; $k++)
				{
					if($menu_list[$i]['col_list'][$j]['id'] == $col_list[$k]["uuid"])
					{
						$menu_list[$i]['col_list'][$j]['col_name'] = $col_list[$k]["col_name"];
					}
				}
			}
		}
		
		for($i = 0 ; $i < count($menu_list) ; $i++)
		{
			for($j = 0 ; $j < count($menu_list[$i]['col_list']) ; $j++)
			{
				for($k = 0 ; $k < count($role_menu_list) ; $k++)
				{
					if($menu_list[$i]['col_list'][$j]['id'] == $role_menu_list[$k]["col_id"] && $menu_list[$i]['uuid'] == $role_menu_list[$k]["menu_id"])
					{
						$menu_list[$i]['col_list'][$j]['check'] = true;
					}
				}
			}
		}
		
		$json["data"]   = $menu_list;
		$json["code"]   = 0;
		$json["msg"]    = "";
		$this->ajaxReturn($json,"json");
		
	}

    public function get_dept_list()
    {
        $list = M("comm.sys_dept_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }
	
	public function get_role_list()
	{
	    $list = M("comm.sys_role_list")->select();
	    $json["data"]   = $list;
	    $json["code"]   = 0;
	    $json["msg"]    = "";
	    $this->ajaxReturn($json,"json");
	}
	
	public function get_user_dept_list()
	{
		$uuid = I('post.uuid');
		$user_info = M('comm.sys_users')->where("uuid = '$uuid'")->find();
		$dept_list = $user_info['dept_list'];
		$dept_list = explode(',',$dept_list);
		$user_info['dept_list'] = $dept_list;
		$json["data"]   = $user_info;
		$this->ajaxReturn($json,"json");
	}
	
	public function save_user_menu_list()
	{
		//$uuid = I('post.uuid');
		$data = I('post.');
		$uuid = I('post.uuid');
		$col = I('post.col');
		
		if(!empty($col['allchoose']))
		{
			unset($col['allchoose']);
		}
		
		M('comm.sys_role_menu_list')->where("role_id = '$uuid'")->delete();
		
		foreach($col as $key=>$value)
		{
			$key_arr = explode('-',$key);
			$menu_id = $key_arr[0];
			$col_id = $key_arr[1];
			$data['uuid'] = uuid();
			$data['menu_id'] = $menu_id;
			$data['col_id'] = $col_id;
			$data['role_id'] = $uuid;
			
			$result = M('comm.sys_role_menu_list')->add($data);
		}
		
		if($result !== false)
		{
		    $json["code"] = 0;
		    $json["msg"] = "修改成功";
		}
		else
		{
		    $json["code"] = -1;
		    $json["msg"] = "修改失败";
		}
		$this->ajaxReturn($json,"json");
	}
	
	public function get_user_role_menu_list()
	{
		$role_id = I('post.role_id');
		$sql = "select * from COMM.SYS_ROLE_MENU_LIST rm,COMM.SYS_MENU_LIST m where   RM.ROLE_ID = '".$role_id."' and M.UUID = RM.MENU_ID";
		$list = M()->query($sql);
		
		$data['menu1'] = [];
		$data['menu2'] = [];
		$data['menu3'] = [];
		$data['menu4'] = [];
		
		
		
		for($i = 0 ; $i < count($list) ; $i++)
		{
			if($list[$i]['p_menu_name'] == "基础设置")
			{
				$data['menu1'][] = $list[$i];
			}
		}
		
		$json["data"] = $list;
		$json["code"] = 0;
		$json["msg"] = "修改成功";
		$this->ajaxReturn($json,"json");
	}

}